package com.qf.controller;

import com.qf.form.SmsInterfaceForm;
import com.qf.service.InterfaceService;
import com.qf.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dell
 * @version V1.0
 * @Date 2022/6/2 11:23:43
 */
@RestController
@RequestMapping("/sms")
public class InterfaceController {

    @Autowired
    private InterfaceService interfaceService;

    @Value("${ip-header")
    private List<String> ipHeaders;

    @RequestMapping("/interface")
    public Result smsInterface(SmsInterfaceForm smsInterfaceForm, HttpServletRequest request){

        //客户端的ip
        String ip = getReallyIpAddress(request);
        smsInterfaceForm.setIp(ip);

        //校验用户信息合法  clientId 必须存在
        //密码合法
        //ip合法
        //发送信息长度
        //SmsInterfaceForm 内的属性 不能为空
        Map result = checkNotNull(smsInterfaceForm);
        if (result != null){
            //有空值
            return Result.ok(result);
        }
        // smsInterfaceForm  是否正确校验  写在service层
        result = interfaceService.check(smsInterfaceForm);

        return Result.ok(result);

    }

    //非空校验
    private Map checkNotNull(SmsInterfaceForm smsInterfaceForm) {

        Map result = new HashMap();

        String clientID = smsInterfaceForm.getClientID();
        if (StringUtils.isEmpty(clientID)){
            result.put("code", "101");
            result.put("mag", "认证错误， clientId 不能为空");
            return result;
        }

        if (StringUtils.isEmpty(smsInterfaceForm.getPwd())){
            result.put("code", "102");
            result.put("mag", "认证错误， 密码  不能为空");
            return result;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getMobile())){
            result.put("code", "101");
            result.put("mag", "认证错误， 手机号  不能为空");
            return result;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getContent())){
            result.put("code", "104");
            result.put("mag", "认证错误， 消息内容  不能为空");
            return result;
        }
        if (StringUtils.isEmpty(smsInterfaceForm.getIp())){
            result.put("code", "103");
            result.put("mag", "认证错误， IP地址 不能为空");
            return result;
        }
        /*if (StringUtils.isEmpty(smsInterfaceForm.getSrcID())){
            result.put("code", "101");
            result.put("mag", "认证错误， 下行编号  不能为空");
            return result;
        }*/


        return null;
    }

    private String getReallyIpAddress(HttpServletRequest request){
        for (String ipHeader : ipHeaders) {
            if (!StringUtils.isEmpty(ipHeader)&& ipHeader.length() > 0 ){
                String ip = request.getHeader(ipHeader);
                if (!StringUtils.isEmpty(ip) && !"unknown".equalsIgnoreCase(ip)){
                    return ip;
                }
            }
        }
        return request.getRemoteAddr();
    }


}
